package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.charitable.CharitableSpecialEvent;
import com.yanqu.road.entity.config.charitable.CharitableWordConfig;
import com.yanqu.road.entity.config.charitable.CharitableWorkConsumeConfig;
import com.yanqu.road.entity.enums.eCharitableSpecialEventRewardType;
import com.yanqu.road.logic.bussiness.config.CharitableBusiness;
import com.yanqu.road.server.TempMgr;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Author wpj
 * @Date 2020/8/29
 */
@ConfigMgrMark
public class CharitableWorkMgr extends TempMgr {

    private static Map<Integer, CharitableWordConfig> charitableWordConfigMap;
    private static Map<Integer, CharitableWorkConsumeConfig> charitableWorkConsumeConfigMap;

    @Override
    public boolean reloadConfig() throws Exception {
        charitableWordConfigMap = CharitableBusiness.getCharitableWordConfig();
        charitableWorkConsumeConfigMap = CharitableBusiness.getCharitableWorkConsumeConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static Map<Integer, CharitableWordConfig> getCharitableWordConfigMap() {
        return charitableWordConfigMap;
    }

    public static Map<Integer, CharitableWorkConsumeConfig> getCharitableWorkConsumeConfigMap() {
        return charitableWorkConsumeConfigMap;
    }

    /**
     * 获取一个随机事件
     * @param specialGuideList
     * @return
     */
    public static CharitableSpecialEvent getRandomSpecialEvent(List<CharitableSpecialEvent> specialGuideList, boolean reward) {
        int allWeight = 0;
        for (CharitableSpecialEvent charitableSpecialEvent : specialGuideList) {
            if (reward && charitableSpecialEvent.getRewardType() == eCharitableSpecialEventRewardType.NotEvent) {
                continue;
            }
            allWeight += charitableSpecialEvent.getWeight();
        }

        int randomNum = new Random().nextInt(allWeight) + 1;

        int nowWeight = 0;
        for (CharitableSpecialEvent charitableSpecialEvent : specialGuideList) {
            if (reward && charitableSpecialEvent.getRewardType() == eCharitableSpecialEventRewardType.NotEvent) {
                continue;
            }
            nowWeight += charitableSpecialEvent.getWeight();
            if (nowWeight >= randomNum) {
                return charitableSpecialEvent;
            }
        }
        return null;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }
}
